package com.bilibili.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.dto.CommentInfo;
import com.bilibili.entity.CommonResult;
import com.bilibili.entity.Page;
import com.bilibili.service.CommentService;
import com.bilibili.service.impl.CommentServiceImpl;

@SuppressWarnings("serial")
@WebServlet("/queryAllCommentB")
public class QueryAllCommentBServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json; charset=utf-8");
		String state = request.getParameter("state");
		String title = request.getParameter("title");
		String userId = request.getParameter("userId");
		int user_id = -1,status = -1,pageNum = 1,pageSize = 5;
		try{
			user_id = Integer.parseInt(userId);
		}catch (Exception e) {
			// TODO: handle exception
		}
		if(state != null) {
			if(state.equals("正常")) {
				status = 1;
			}else if(state.equals("已删除")) {
				status = 0;
			}else {
				status = -1;
			}
		}
		if(request.getParameter("pageNum") != null){
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		
		if(request.getParameter("pageSize") != null){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		CommentInfo ci = new CommentInfo();
		ci.setState(status);
		ci.setVideoTitle(title);
		ci.setUserId(user_id);
		CommentService cs = new CommentServiceImpl();
		Page<CommentInfo> commentPage = cs.selectAllCommentInfoB(ci, pageNum, pageSize);
		CommonResult cr = new CommonResult();
		if(commentPage.getData().size() != 0) {
			cr.setCode(200);
			cr.setData(commentPage);
		}else {
			cr.setCode(409);
		}
		response.getWriter().write(cr.toJSONString());
	}
}
